/**
 * 省市区联动(对象方式) author：张长兵 date：2016-06-21
 * var xzqh= new xzqhLink({
 *			basePath:"${path}",
 *		    provinceEle:$('#province'),
 *		    cityEle:$('#city'),
 *		    districtEle:$('#district')
 *	});
 */
function xzqhLink(options){
		var settings = $.extend({
			selectPreHtml:"<option value=''></option>", //初始化的第一个
			basePath:"", //
			provinceInitialValue:null,//省初始化id
			cityInitialValue:null,//市初始化id
			districtInitialValue:null,//区县初始化id
			provinceEle:null,//省操作select对象
			cityEle:null,//市操作select对象
			districtEle:null,//区县操作对象
			paramId:'id'
		},options);
		
		
		var queryUrl = '/basicinfo/taxPayersInfo/listByPidCode';//查询数据的url
		
		var prepareSelectHtml=function(jsonArray, selectedId){		
			var temp_html=settings.selectPreHtml;
			if (jsonArray != null && jsonArray.length > 0) {
				$.each(jsonArray,function(index,row){
					if(row.code == selectedId){
						temp_html+="<option selected='selected' value='"+row.code+"'>"+row.name+"</option>";
					}
					else{
						temp_html+="<option value='"+row.code+"'>"+row.name+"</option>";
					}
				});
			}
			return temp_html;
		};
		
		/**
		 * settingsEle 当前操作的select
		 * preEle 上一级的select
		 * settingslevel 地级市等级 0 ：省 1：市 2：区县
		 * initialId 上级初始化的id
		 * initialSelectVal 本级初始化的id
		 */
		var createSelect = function(settingsEle,preEle,initialId, initialSelectVal){
			if(!settingsEle){
				return;
			}
			var params = {};
			if(initialId){
				params[settings.paramId] = initialId;
			}
			else{
				if(preEle.val()){
					params[settings.paramId] = preEle.val();
				}
				else{
					settingsEle.html(settings.selectPreHtml);
					return;
				}
			}
			$.getJSON(settings.basePath+queryUrl, params, function(jsonResult){
				if(jsonResult.code ==  "0000"){
					settingsEle.html(prepareSelectHtml(jsonResult.returnObject.rows, initialSelectVal));         	
				}
				else{
					alert(jsonResult.msg);
				}
				
			});
		};
		
		
		var build = function(){
			var params = {};
			params[settings.paramId] = "000000";//查出所有的省份或直辖市
			$.getJSON(queryUrl, params ,function(jsonResult){
				if(jsonResult.code == "0000"){
					settings.provinceEle.html(prepareSelectHtml(jsonResult.returnObject.rows, settings.provinceInitialValue)); 
					//console.debug(settings.provinceEle);
					//alert(settings.provinceEle.html());
				}
				else{
					//alert(jsonResult.msg);
				}
			});
			
			createSelect(settings.cityEle,settings.provinceEle,settings.provinceInitialValue, settings.cityInitialValue);
			createSelect(settings.districtEle,settings.cityEle,settings.cityInitialValue, settings.districtInitialValue);
			// 选择一级时发生事件
			settings.provinceEle.unbind("change");
			settings.provinceEle.bind("change",function(){
				if(settings.cityEle){
					settings.cityEle.html(settings.selectPreHtml);
				}
				if(settings.districtEle){
					settings.districtEle.html(settings.selectPreHtml);
				}
				createSelect(settings.cityEle,settings.provinceEle);
			});
			// 选择二级时发生事件
			settings.cityEle.unbind("change");
			settings.cityEle.bind("change",function(){
				if(settings.districtEle){
					settings.districtEle.html(settings.selectPreHtml);
				}
				createSelect(settings.districtEle,settings.cityEle);
			});
			
			
		};
		
		build();
}